package com.eboss2.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.TabBasicActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.enums.EnumConsts;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.ShopCollectionInfo;
import com.eboss2.sbs.model.ShopCollectionIsHas;
import com.eboss2.sbs.myapplication.MyApplication;
import com.eboss2.sbs.weight.DialogUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.StringUtil;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.utils.ToastUtils;
import com.tool.utils.utils.UtilPreference;

import org.json.JSONObject;

public class ReceiptActivity extends TabBasicActivity implements View.OnClickListener{

	private final String TAG = "ReceiptActivity";
	private ShopCollectionIsHas vo;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_receipt);
		MyActivityManager.getInstance().addActivity(this);
//		StatusBarUtil.setColor(this, getResources().getColor(R.color.statusBar), 0);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, false);
		setTvText(R.id.tv_title, "收款");

		findViewById(R.id.ll_tx).setVisibility(View.GONE);
		
		findViewById(R.id.layout_barcode_scan).setOnClickListener(this);
		findViewById(R.id.layout_coupon).setOnClickListener(this);
		findViewById(R.id.layout_barcode).setOnClickListener(this);
		findViewById(R.id.layout_account_info).setOnClickListener(this);
		findViewById(R.id.layout_receipt_info).setOnClickListener(this);
		findViewById(R.id.layout_verification).setOnClickListener(this);
		findViewById(R.id.layout_shop_record).setOnClickListener(this);
		findViewById(R.id.layout_shop_trans_count).setOnClickListener(this);
		findViewById(R.id.layout_store_record).setOnClickListener(this);
		findViewById(R.id.layout_sale).setOnClickListener(this);
		findViewById(R.id.layout_credit).setOnClickListener(this);

		getdata();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.layout_barcode_scan:
				pay(1);
				break;
			case R.id.layout_coupon:
				startActivity(new Intent(mContext, VerificationActivity.class));
				break;
			case R.id.layout_barcode:
				showBarcode();
				break;
			case R.id.layout_account_info:
				startActivity(new Intent(mContext, AccountInfoActivity.class));
				break;
			case R.id.layout_receipt_info:
				startActivity(new Intent(mContext, ReceiptInfoActivity.class));
				break;
			case R.id.layout_verification:
				startActivity(new Intent(mContext, VerificationRecordActivity.class));
				break;
			case R.id.layout_shop_record:
				startActivity(new Intent(mContext, ShopRecordActivity.class));
				break;
			case R.id.layout_store_record:
				startActivity(new Intent(mContext, StoreRecordActivity.class));
				break;
			case R.id.layout_shop_trans_count:
				startActivity(new Intent(mContext, ShopTransCountActivity.class));
				break;
			case R.id.layout_sale:
				if (DialogUtils.checkApproveStatus(mContext)) {
					return;
				}
				startActivity(new Intent(mContext, ActivityKjsk.class));
				break;
			case R.id.layout_credit:
				if (DialogUtils.checkApproveStatus(mContext)) {
					return;
				}
				startActivity(new Intent(mContext, ActivitySmartCredit.class));
				break;
			default: break;
		}
	}


	@Override
	protected void onResume() {
		super.onResume();
		loadStatus();
	}

	private void loadStatus() {

//		showProgress("加载中...");
		RequestParams params = new RequestParams();
		params.add("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid() + "");
		HttpUtil.get(Config.SBS_COLLECTION_IS_HAS, params, new HttpUtil.RequestListener() {
			@Override
			public void success(String response) {
				disShowProgress();
				try {
					JSONObject result = new JSONObject(response);

					if (!result.optBoolean("status")) {
						ToastUtils.CustomShow(mContext, result.optString("message"));
					} else {
						ShopCollectionIsHas shopCollectionIsHas = new Gson().fromJson(result.optString("data"), new TypeToken<ShopCollectionIsHas>() {
						}.getType());
						if (shopCollectionIsHas != null) {
							vo = shopCollectionIsHas;
						}
					}
				} catch (Exception e) {
					Log.e(TAG, "doLogin() Exception: " + e.getMessage());
				}


			}

			@Override
			public void failed(Throwable error) {
				disShowProgress();
				showE404();
			}
		});
	}


	private void getdata() {

		showProgress("加载中...");
		RequestParams params = new RequestParams();
		params.add("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid() + "");
		HttpUtil.get(Config.SBS_COLLECTION_INFO, params, new HttpUtil.RequestListener() {
			@Override
			public void success(String response) {
				disShowProgress();
				try {
					JSONObject result = new JSONObject(response);

					if (!result.optBoolean("status")) {
						ToastUtils.CustomShow(mContext, result.optString("message"));
					} else {
						ShopCollectionInfo shopCollectionInfo = new Gson().fromJson(result.optString("data"), new TypeToken<ShopCollectionInfo>() {
						}.getType());
						if (shopCollectionInfo != null) {
							//保存商户号
							UtilPreference.saveString(mContext, "mid", shopCollectionInfo.getScanMerchantId());
							UtilPreference.saveString(mContext, "tid", shopCollectionInfo.getScanTerminalId());
							UtilPreference.saveString(mContext, "merchantName", shopCollectionInfo.getScanMerchantName());
							UtilPreference.saveInt(mContext, "scanOrganization", shopCollectionInfo.getScanOrganization());
						}
					}
				} catch (Exception e) {
					Log.e(TAG, "doLogin() Exception: " + e.getMessage());
				}


			}

			@Override
			public void failed(Throwable error) {
				disShowProgress();
				showE404();
			}
		});
	}
	
	private void pay(int type){
//		String activateCode = LocalParasUtils.getActivateCode(mContext);
//		if(!mApp.getLoginEntity().getCanCheckout()){
//			toast("当前不支持终端收款，请联系客服");
//		}
//		else if(mApp.getLoginEntity().getScanPayType()== EnumConsts.ScanPayType.SQB.getType()&& StringUtil.isNull(activateCode)){
//			toast("终端未激活，请联系客服");
//		}
//		else if(mApp.getLoginEntity().getScanPayType()== EnumConsts.ScanPayType.FUYOU.getType()&&StringUtil.isNull(mApp.getLoginEntity().getFyMerchantNo())){
//			toast("商户号未配置，请联系客服");
//		}
//		else if((mApp.getLoginEntity().getScanPayType()== EnumConsts.ScanPayType.SQB.getType()&&!StringUtil.isNull(activateCode))||
//				(mApp.getLoginEntity().getScanPayType()== EnumConsts.ScanPayType.FUYOU.getType()&&!StringUtil.isNull(mApp.getLoginEntity().getFyMerchantNo()))){
//			if(type==1){
//				startActivity(new Intent(mActivity, PayMoneyInputActivity.class));
//			}
//			else if(type==2){
//				showBarcode();
//			}
//		}
//		else{
//			toast("终端未配置成功，请联系客服");
//		}

		if (vo == null){
			return;
		}

		if (vo.getDeviceStatus() != EnumConsts.Device.Normal.getType()){
			ToastUtils.showShort(mContext, EnumConsts.Device.getByType(vo.getDeviceStatus()).getName());
			return;
		}
		if (!vo.isCashierModeScan()){
			ToastUtils.showShort(mContext, "请确认是否开通收款");
			return;
		}



		String mid = UtilPreference.getStringValue(mContext, "mid");
		if (StringUtils.isBlank(mid)){
			ToastUtils.showShort(mContext, "商户号未配置，请联系客服");
			return;
		}
		startActivity(new Intent(mContext, PayMoneyInputActivity.class));
	}
	
	private void showBarcode(){

		if (vo == null){
			return;
		}

		if (vo.getDeviceStatus() != EnumConsts.Device.Normal.getType()){
			ToastUtils.showShort(mContext, EnumConsts.Device.getByType(vo.getDeviceStatus()).getName());
			return;
		}

		if (!vo.isCashierModeDeskcard()){
			ToastUtils.showShort(mContext, "请确认是否开通台卡");
			return;
		}


		showProgress("加载中...");
		RequestParams params = new RequestParams();
		params.add("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid()+"");
		HttpUtil.get(Config.SBS_QR, params, new HttpUtil.RequestListener() {
			@Override
			public void success(String response) {
				disShowProgress();
				try {
					JSONObject result = new JSONObject(response);

					if (!result.optBoolean("status")) {
						ToastUtils.CustomShow(mContext, result.optString("message"));
					}else{
						JSONObject dataStr = result.getJSONObject("data");

						String barcode = dataStr.optString("url");
						if(StringUtil.isNull(barcode)){
//							toast("商户未设置收款二维码");
							ToastUtils.showShort(mContext, "商户未设置收款二维码");
						}
						else{
							Intent intent =  new Intent(mContext, ReceiptBarcodeActivity.class);
							intent.putExtra("barcode", barcode);
							intent.putExtra("shopName", dataStr.optString("shopName"));
							startActivity(intent);
						}
					}
				} catch (Exception e) {
					Log.e(TAG, "doLogin() Exception: " + e.getMessage());
				}


			}

			@Override
			public void failed(Throwable error) {
				disShowProgress();
				showE404();
			}
		});
		
//		Map<String, Object> params = new HashMap<String, Object>();
////		params.put("sid", mApp.getLoginEntity().getSid());
//		API.http_post(this, API.PAY_BARCORD, params, new HttpCallback<PayBarcodeEntity>(this) {
//			@Override
//			public void onSuccess(int statusCode, PayBarcodeEntity data) {
//
//				String barcode = data.getBarcode();
//				if(StringUtil.isNull(barcode)){
//					toast("商户未设置收款二维码");
//				}
//				else{
//					Intent intent =  new Intent(mContext, ReceiptBarcodeActivity.class);
//					intent.putExtra("barcode", barcode);
//					startActivity(intent);
//				}
//			}
//
//			@Override
//			public void onFailure(int statusCode, String message) {
//				super.onFailure(statusCode, message);
//			}
//
//			@SuppressWarnings({ "unchecked", "rawtypes" })
//			@Override
//			public TypeToken getTypeToken() {
//				return new TypeToken<PayBarcodeEntity>() {
//				};
//			}
//		});
		
	}
	
	
	
}
